<template>
  <div class="app-container">
    <ResizeBox :treeData="treeData" @pitchOnData="pitchOnData">
      <template>
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          v-show="showSearch"
          label-width="100px"
        >
          <el-form-item label="停工开始时间" prop="beginTimeList">
            <el-date-picker
              v-model="queryParams.beginTimeList"
              type="datetimerange"
              value-format="yyyy-MM-dd HH:mm:ss"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              align="right"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="停工结束时间" prop="endTimeList">
            <el-date-picker
              v-model="queryParams.endTimeList"
              type="datetimerange"
              value-format="yyyy-MM-dd HH:mm:ss"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              align="right"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['syf:record:export']"
              >导出</el-button
            >
          </el-col>
          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>

        <el-table
          v-loading="loading"
          :data="recordList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            label="序号"
            type="index"
            align="center"
            width="60"
          />
          <el-table-column label="风险对象" align="center" prop="objectName" />
          <el-table-column label="风险单元" align="center" prop="unitName" />
          <el-table-column
            label="停工开始时间"
            align="center"
            prop="beginTime"
            width="180"
          >
            <template slot-scope="scope">
              <span>{{
                parseTime(scope.row.beginTime, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="停工结束时间"
            align="center"
            prop="endTime"
            width="180"
          >
            <template slot-scope="scope">
              <span>{{
                parseTime(scope.row.endTime, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="原因"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-tickets"
                @click="selectReason(scope.row)"
                v-hasPermi="['syf:object:edit']"
                >查看原因</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </template>
    </ResizeBox>
  </div>
</template>

<script>
import { listRecord, getRecord } from "@/api/syf/risk/record";
import { unitTree } from "@/api/syf/tree";

import ResizeBox from "@/components/syf/ResizeBox/index";

export default {
  name: "Record",
  components: { ResizeBox },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 开停工记录表格数据
      recordList: [],
      // 树选项
      treeData: undefined,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        objectId: null,
        unitId: null,
        beginTimeList: null,
        endTimeList: null,
      },
    };
  },
  created() {
    this.getTreeData();
    this.getList();
  },
  methods: {
    /** 查询开停工记录列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then((response) => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "syf/record/export",
        {
          ...this.queryParams,
        },
        `record_${new Date().getTime()}.xlsx`
      );
    },
    /** 查询树数据 */
    getTreeData() {
      unitTree().then((res) => {
        this.treeData = res.data;
      });
    },
    //选中树节点
    pitchOnData(val) {
      this.handleQuery();
    },
    //查看原因
    selectReason(row) {
      getRecord(row.id).then((response) => {
        const reason = response.data.reason;
        this.$alert(reason, "停工原因");
      });
    },
  },
};
</script>
